package pers.youqu.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.youqu.common.exception.YqException;
import pers.youqu.item.dto.SpecGroupDTO;
import pers.youqu.item.mapper.SpecGroupMapper;
import pers.youqu.item.po.SpecGroup;
import pers.youqu.item.service.SpecGroupService;
import pers.youqu.item.service.SpecParamService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Jason
 * @version 1.0.0
 * @date 2020/12/6 21:29
 * @Description TODO
 */
@Service
public class SpecGroupServiceImpl extends ServiceImpl<SpecGroupMapper, SpecGroup> implements SpecGroupService {
    @Autowired
    private SpecParamService specParamService;

    @Override
    public List<SpecGroupDTO> findSpecGroupByBrandId(Long cId) {
        return SpecGroupDTO.convertEntityList(query().eq("category_id", cId).list());
    }

    @Override
    public void addSpecGroup(SpecGroupDTO specGroupDTO) {
        boolean flag = this.save(specGroupDTO.toEntity(SpecGroup.class));
        if (!flag) {
            throw new YqException(500, "新增规格组错误");
        }
    }

    @Override
    public void updateSpecGroup(SpecGroupDTO specGroupDTO) {
        boolean flag = updateById(specGroupDTO.toEntity(SpecGroup.class));
        if (!flag) {
            throw new YqException(500, "修改规格组错误");
        }
    }

    @Override
    public List<SpecGroupDTO> findSpecGroupListByCid(Long cid) {
        List<SpecGroupDTO> specGroupDTOS = findSpecGroupByBrandId(cid);
        specGroupDTOS.stream().map(specGroupDTO -> {
            specGroupDTO.setParams(specParamService.findSpecParam(cid, specGroupDTO.getId(), null));
            return specGroupDTO;
        }).collect(Collectors.toList());

        return specGroupDTOS;
    }
}
